{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      },
      "source": [
        "# Adapting Qiskit samples to run on Azure Quantum\n",
        "\n",
        "If you have some experience with quantum computing or are just starting, it is likely that you will use some samples from Qiskit.org. This Jupyter notebook walks you through 4 simple steps to adapt an existing Qiskit sample to run against any of the Azure Quantum back-ends.\n",
        "\n",
        "Most samples are configured to run by default against the aer_simulator, which is a great way to get started.\n",
        "\n",
        "The aer_simulator is a local simulator, extremely performant, but different from a cloud simulator. When a local simulator is used, all jobs are computed \"in memory\" and there is no network latency nor job queue compared to an actual cloud backend (simulator or QPU). It enables you to run these simulations very quickly, but they may not accurately represent the behavior of a cloud backend.\n",
        "\n",
        "When you don't use a local simulator, you may experience total experiment duration that are orders of magnitude longer (from minutes for a local simulator, to hours or days for an actual back-end). \n",
        "\n",
        "Additionally a local simulator only uses the 'local' compute or a hosted compute. When using a cloud backend you may be spending money or credits, depending on what you selected.\n",
        "\n",
        "Make sure to perform tests on smaller experiments when selecting cloud backends."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      },
      "source": [
        "# 1. Download a sample from Qiskit.org\n",
        "Let's say you want to try the QAOA sample: https://qiskit.org/textbook/ch-applications/qaoa.html\n",
        "\n",
        "1. Locate the **Download as Jupyter Notebook** hyperlink on the right side of the page\n",
        "2. Download the Jupyter Notebook\n",
        "3. Open your Azure Quantum workspace > **Notebooks**\n",
        "4. Click **My notebooks** > ...\n",
        "5. Click on the **Upload new** button and select the notebook your just downloaded\n",
        "6. Open the newly imported notebook"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      },
      "source": [
        "# 2. Locate the aer_simulator\n",
        "\n",
        "1. Locate the cell that has a line like the following:\n",
        "**backend = Aer.get_backend('aer_simulator')**\n",
        "2. Comment out that line"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      },
      "source": [
        "# 3. Replace the aer_simulator with an Azure Quantum backend\n",
        "\n",
        "1. Add a new code cell prior to the cell that had backend = Aer.get_backend('aer_simulator')\n",
        "2. Add the following code\n",
        "\n",
        "```\n",
        "from azure.quantum.qiskit import AzureQuantumProvider\n",
        "provider = AzureQuantumProvider (\n",
        "    resource_id = \"\",\n",
        "    location = \"\"\n",
        ")\n",
        "\n",
        "# Create IonQ simulator and QPU backends\n",
        "ionq_simulator_backend = provider.get_backend(\"ionq.simulator\")\n",
        "ionq_qpu_backend = provider.get_backend(\"ionq.qpu.aria-1\")\n",
        "\n",
        "# Create Rigetti simulator and QPU backends\n",
        "rigetti_simulator_backend = provider.get_backend(\"rigetti.sim.qvm\")\n",
        "rigetti_qpu_backend = provider.get_backend(\"rigetti.qpu.aspen-m-3\")\n",
        "\n",
        "# Create Quantinuum simulator and QPU backends\n",
        "quantinuum_simulator_backend = provider.get_backend(\"quantinuum.sim.h1-1sc\")\n",
        "quantinuum_qpu_backend = provider.get_backend(\"quantinuum.qpu.h1-1\")\n",
        "\n",
        "backend = ionq_simulator_backend\n",
        "```\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      },
      "source": [
        "# 4. Update code with your workspace information\n",
        "\n",
        "1. Make sure you save your notebook. Click **Save**. Never too sure :)\n",
        "2. In the left pane of your Azure Quantum workspace, click **Overview**\n",
        "3. In the top right, copy the Resource Id, by clickin on the **Copy to clipboard** icon that appears when you hover it.\n",
        "4.Memorize the **Location** (for instance 'West Europe')\n",
        "5. Open again your notebook (Notebooks > My notebooks)\n",
        "6. Add the Resource Id to **resource_id** in the AzureQuantumProvider\n",
        "7. Repeat for the **location** using the Location you memorized in step 4."
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 4. Install Python packages\n",
        "\n",
        "When working in the Azure Quantum hosted notebooks, you benefit from some pre-installed python packages to ease your experience, but not all the one you need may be present.\n",
        "To install additional Python packages, follow these steps:\n",
        "\n",
        "1. Add a code cell\n",
        "1. Add the package to be installed using this command:\n",
        "!pip install [package_name]\n",
        "for instance\n",
        "!pip install torch\n",
        "1. Restart the Kernel\n",
        "\n",
        "Note as well that some packages may be very large and could be best suited to bet setup in your local environment."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      },
      "source": [
        "# You are ready!\n",
        "Do a **Run all** and verify that you have jobs being queued under Job management."
      ]
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "python3"
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.9"
    },
    "nteract": {
      "version": "nteract-front-end@1.0.0"
    },
    "vscode": {
      "interpreter": {
        "hash": "b6bcd221c9cd9ce3727161d70256b3c0b2b563aa4bdf9e9f2833e204c83333df"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
